#!/bin/sh

# #######################################################
# This script makes assumptions about your JAVA_HOME,
# ANT_HOME and your SunStudio home.  Please replace these 
# values in the environment variable declarations below.
# Also, on Nexenta, use gcc as your CC_FOR_SWT and GCC
# as your CXX_FOR_SWT.
# #######################################################

# unzip -q eclipse-sourceBuild-srcIncluded-3.3.zip

# #######################################################
# set environment variables
# #######################################################
JAVA_HOME=/usr/java
export JAVA_HOME
ANT_HOME=/export/home/timezra/opt/ant
export ANT_HOME
PATH=$PATH:$ANT_HOME/bin:/usr/local/bin
export PATH
SUN_STUDIO_HOME=/opt/SUNWspro
export SUN_STUDIO_HOME
CC_FOR_SWT=$SUN_STUDIO_HOME/bin/cc
export CC_FOR_SWT
CXX_FOR_SWT=$SUN_STUDIO_HOME/bin/CC
export CXX_FOR_SWT
ASSEMBLY_VERSION=`grep "/org\.eclipse\.swt\.gtk\.solaris\.sparc_" ./assemble.org.eclipse.sdk.solaris.gtk.sparc.xml | sed "s/.*org\.eclipse\.swt\.gtk\.solaris\.sparc_//" | sed "s/\.jar.*//" | sed "s/.*\.//"`
export ASSEMBLY_VERSION


# #######################################################
# alter the swt build scripts
# #######################################################

perl -pi -e 's~all: make_swt make_atk make_awt make_cde~all: make_swt make_atk make_awt make_gnome~' ./plugins/org.eclipse.swt/Eclipse\ SWT\ PI/gtk/library/make_solaris.mak

# For the other build scripts, we will use the sun cc compiler
# For the swt libraries, however, we need to use the gcc compiler
# Originally I was using the sun cc compiler for the swt libraries
# but Eclipse crashed with the error
# libswt-gnome-gtk-3349.so: symbol _mcount: referenced symbol not found
# apparently because the gcc linker does some extra magic.

perl -pi -e 's~SWT_VERSION=\$\(maj_ver\)\$\(min_ver\)~SWT_VERSION=\$\(maj_ver\)\$\(min_ver\)\nJAVA_HOME=$ENV{JAVA_HOME}\nCC=$ENV{CC_FOR_SWT}\n~' ./plugins/org.eclipse.swt/Eclipse\ SWT\ PI/gtk/library/make_solaris.mak

perl -pi -e 's~^\s*MAKE_MOZILLA=make_mozilla~#\t\tMAKE_MOZILLA=make_mozilla~g' ./plugins/org.eclipse.swt/Eclipse\ SWT\ PI/gtk/library/build.sh

perl -pi -e 's~fileset dir="\$\{plugindir\}/Eclipse SWT Program/cde/library/"~fileset dir="\$\{plugindir\}/Eclipse SWT PI/common/library/"~' ./plugins/org.eclipse.swt.gtk.solaris.x86/build.xml

# #######################################################
# alter the launcher build scripts
# #######################################################

perl -pi -e 's~org.eclipse.equinox.executable/contributed/\$\{installWs\}/\$\{installOs\}/.\$\{installArch\}/" />~org.eclipse.equinox.executable/contributed/\$\{installWs\}/\$\{installOs\}/\$\{installArch\}/" />~' ./build.xml

perl -pi -e 's~<fileset dir="\$\{basedir\}/../../launchertmp">~<fileset dir="\$\{basedir\}/../../features/org.eclipse.equinox.executable/contributed/gtk/solaris/x86">~' ./features/org.eclipse.platform/build.xml

perl -pi -e 's~#\s*JAVA_HOME\s*-\s*JAVA_HOME for JNI headers~JAVA_HOME=$ENV{JAVA_HOME}\nCC=$ENV{SUN_STUDIO_HOME}/bin/cc\n~' ./features/org.eclipse.equinox.executable/library/gtk/make_solaris.mak

cp -r ./plugins/org.eclipse.equinox.launcher.gtk.solaris.sparc ./plugins/org.eclipse.equinox.launcher.gtk.solaris.x86

mv ./plugins/org.eclipse.equinox.launcher.gtk.solaris.x86/launcher.gtk.solaris.sparc.properties ./plugins/org.eclipse.equinox.launcher.gtk.solaris.x86/launcher.gtk.solaris.x86.properties

perl -pi -e 's~sparc~x86~g' ./plugins/org.eclipse.equinox.launcher.gtk.solaris.x86/build.xml ./plugins/org.eclipse.equinox.launcher.gtk.solaris.x86/build.properties ./plugins/org.eclipse.equinox.launcher.gtk.solaris.x86/launcher.gtk.solaris.x86.properties ./plugins/org.eclipse.equinox.launcher.gtk.solaris.x86/META-INF/MANIFEST.MF

perl -pi -e 's~Sparc~X86~g' ./plugins/org.eclipse.equinox.launcher.gtk.solaris.x86/launcher.gtk.solaris.x86.properties

cp -r ./plugins/org.eclipse.equinox.launcher/fragments/org.eclipse.equinox.launcher.gtk.solaris.sparc ./plugins/org.eclipse.equinox.launcher/fragments/org.eclipse.equinox.launcher.gtk.solaris.x86

mv ./plugins/org.eclipse.equinox.launcher/fragments/org.eclipse.equinox.launcher.gtk.solaris.x86/launcher.gtk.solaris.sparc.properties ./plugins/org.eclipse.equinox.launcher/fragments/org.eclipse.equinox.launcher.gtk.solaris.x86/launcher.gtk.solaris.x86.properties

perl -pi -e 's~sparc~x86~g' ./plugins/org.eclipse.equinox.launcher/fragments/org.eclipse.equinox.launcher.gtk.solaris.x86/build.properties ./plugins/org.eclipse.equinox.launcher/fragments/org.eclipse.equinox.launcher.gtk.solaris.x86/META-INF/MANIFEST.MF

perl -pi -e 's~Sparc~X86~g' ./plugins/org.eclipse.equinox.launcher/fragments/org.eclipse.equinox.launcher.gtk.solaris.x86/launcher.gtk.solaris.x86.properties

# rebuild the launchersrc.zip

mkdir launchertmp

mv ./plugins/org.eclipse.platform/launchersrc.zip launchertmp

cd launchertmp

unzip -q launchersrc.zip

perl -pi -e 's~#\s*JAVA_HOME\s*-\s*JAVA_HOME for JNI headers~JAVA_HOME=$ENV{JAVA_HOME}\nCC=$ENV{SUN_STUDIO_HOME}/bin/cc\n~' ./library/gtk/make_solaris.mak

cd library/gtk

sh build.sh

cp eclipse_*.so ../../../plugins/org.eclipse.equinox.launcher.gtk.solaris.x86

cp eclipse_*.so ../../../plugins/org.eclipse.equinox.launcher/fragments/org.eclipse.equinox.launcher.gtk.solaris.x86

cd ../..

rm launchersrc.zip

zip -r launchersrc.zip *

mv launchersrc.zip ../plugins/org.eclipse.platform

cd ..

rm -r launchertmp

# #######################################################
# alter the filesystem build scripts
# #######################################################

perl -pi -e 's~plugins/org.eclipse.core.filesystem/natives/unix/linux~plugins/org.eclipse.core.filesystem/natives/unix/\$\{installOs\}~' ./build.xml

perl -pi -e 's~plugins/org.eclipse.core.filesystem.linux.\$\{installArch\}/os/linux/\$\{installArch\}~plugins/org.eclipse.core.filesystem.\$\{installOs\}.\$\{installArch\}/os/\$\{installOs\}/\$\{installArch\}~' ./build.xml

perl -pi -e 's~JAVA_HOME = \~/vm/sun142~JAVA_HOME=$ENV{JAVA_HOME}\nCC=$ENV{SUN_STUDIO_HOME}/bin/cc\n~' ./plugins/org.eclipse.core.filesystem/natives/unix/solaris/Makefile

perl -pi -e 's~sparc~x86~g' ./plugins/org.eclipse.core.filesystem/natives/unix/solaris/Makefile

cp -r ./plugins/org.eclipse.core.filesystem.solaris.sparc ./plugins/org.eclipse.core.filesystem.solaris.x86

mv ./plugins/org.eclipse.core.filesystem.solaris.x86/os/solaris/sparc ./plugins/org.eclipse.core.filesystem.solaris.x86/os/solaris/x86

cd ./plugins/org.eclipse.core.filesystem/natives/unix/solaris

make

cp liblocalfile_1_0_0.so ../../../../org.eclipse.core.filesystem.solaris.x86/os/solaris/x86

cd ../../../../..

perl -pi -e 's~sparc~x86~g' ./plugins/org.eclipse.core.filesystem.solaris.x86/build.xml ./plugins/org.eclipse.core.filesystem.solaris.x86/META-INF/MANIFEST.MF ./plugins/org.eclipse.core.filesystem.solaris.x86/BUILD_INFO.txt

perl -pi -e 's~Sparc~X86~g' ./plugins/org.eclipse.core.filesystem.solaris.x86/fragment.properties

perl -pi -e 's~make install JAVA_HOME=\$TOOLSRV/jdk/1.5.0_08/SunOS/5.9~make install JAVA_HOME=$ENV{JAVA_HOME}~' ./plugins/org.eclipse.core.filesystem.solaris.x86/BUILD_INFO.txt

perl -pi -e 's~setenv PATH \$TOOLSRV/gcc/3.4.4/SunOS/5.5/bin:\$PATH~setenv PATH $ENV{SUN_STUDIO_HOME}/bin:\$PATH~' ./plugins/org.eclipse.core.filesystem.solaris.x86/BUILD_INFO.txt

# #######################################################
# alter the update build scripts
# #######################################################

perl -pi -e 's~plugins/org.eclipse.update.core.linux/src~plugins/org.eclipse.update.core.\$\{installOs\}/src~' ./build.xml

cp -r ./plugins/org.eclipse.update.core.linux ./plugins/org.eclipse.update.core.solaris

mv ./plugins/org.eclipse.update.core.solaris/os/linux ./plugins/org.eclipse.update.core.solaris/os/solaris

perl -pi -e 's~linux~solaris~g' ./plugins/org.eclipse.update.core.solaris/build.xml ./plugins/org.eclipse.update.core.solaris/src/build.xml ./plugins/org.eclipse.update.core.solaris/META-INF/MANIFEST.MF

perl -pi -e 's~Linux~Solaris~g' ./plugins/org.eclipse.update.core.solaris/META-INF/MANIFEST.MF

perl -pi -e 's~gcc -o \$\{library-file\} -shared -I\$\{src-path\} -I\$\{header-solaris-path\} -fPIC \$\{library-file\}~$ENV{SUN_STUDIO_HOME}/bin/cc -o \$\{library-file\} -shared -I\$\{src-path\} -I\$\{header-solaris-path\} -KPIC \$\{library-file\}~' ./plugins/org.eclipse.update.core.solaris/src/build.xml

perl -pi -e 's~apply executable="gcc"~apply executable="$ENV{SUN_STUDIO_HOME}/bin/cc"~' ./plugins/org.eclipse.update.core.solaris/src/build.xml

perl -pi -e 's~arg value="-fPIC"~arg value="-KPIC"~' ./plugins/org.eclipse.update.core.solaris/src/build.xml

perl -pi -e 's~statfs~statvfs~' ./plugins/org.eclipse.update.core.solaris/src/update.c

# #######################################################
# use the solaris-gtk-sparc platform source fragment
# since that has been maintained
# #######################################################

rm -r ./plugins/org.eclipse.platform.source.solaris.gtk.x86

cp -r ./plugins/org.eclipse.platform.source.solaris.gtk.sparc ./plugins/org.eclipse.platform.source.solaris.gtk.x86

perl -pi -e 's~sparc~x86~g' ./plugins/org.eclipse.platform.source.solaris.gtk.x86/build.xml ./plugins/org.eclipse.platform.source.solaris.gtk.x86/META-INF/MANIFEST.MF

# #######################################################
# use the solaris-gtk-sparc assemble and package scripts
# since those have been maintained
# #######################################################

rm assemble.org.eclipse.sdk.solaris.gtk.x86.xml

rm package.org.eclipse.sdk.solaris.gtk.x86.xml

cp assemble.org.eclipse.sdk.solaris.gtk.sparc.xml assemble.org.eclipse.sdk.solaris.gtk.x86.xml

cp assemble.org.eclipse.sdk.solaris.gtk.sparc.xml assemble.org.eclipse.sdk.solaris.gtk.x86.xml

cp package.org.eclipse.sdk.solaris.gtk.sparc.xml package.org.eclipse.sdk.solaris.gtk.x86.xml

perl -pi -e 's~sparc~x86~g' assemble.org.eclipse.sdk.solaris.gtk.x86.xml package.org.eclipse.sdk.solaris.gtk.x86.xml

perl -pi -e 's~org.eclipse.swt.gtk.solaris.x86_3.3.2.$ENV{ASSEMBLY_VERSION}~org.eclipse.swt.gtk.solaris.x86_3.3.0.HEAD~g' assemble.org.eclipse.sdk.solaris.gtk.x86.xml

# #######################################################
# Alter the doc building script to overwrite when the
# executable is unzipped.
# #######################################################

perl -pi -e 's~<arg line="-qq \$\{buildDirectory\}/\$\{buildLabel\}/\*.zip" />~<arg line="-qq -o \$\{buildDirectory\}/\$\{buildLabel\}/\*.zip" />~g' ./build.xml

# #######################################################
# Build Eclipse and Compile the Native Libraries.
# #######################################################

./build -os solaris -ws gtk -arch x86 -compilelibs
